package com.kason.framework.protocol.http;

import com.kason.framework.Invocation;
import com.kason.framework.regester.LocalRegester;
import org.apache.commons.io.IOUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.lang.reflect.Method;

/**
 * @author 卢清波
 * @date 2021/2/19 17:47
 */
public class HttpServerHandler
{

    /**
     * @param req
     * @param resp
     * @throws Exception
     */
    public void Handler(HttpServletRequest req, HttpServletResponse resp) throws Exception
    {
        try
        {
            ObjectInputStream ois = new ObjectInputStream(req.getInputStream());
            Invocation invocation = (Invocation) ois.readObject();
            // 通过方法名，获取本地注册服务类
            String interfaceName = invocation.getInterfaceName();
            Class aClass = LocalRegester.get(interfaceName);
            Method method = aClass.getMethod(invocation.getMethodName(), invocation.getArgTypes());
            Object invoke = method.invoke(aClass.newInstance(), invocation.getArgs());
            IOUtils.write((String)invoke,resp.getOutputStream());
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }



}
